import { z } from 'zod';

/**
 * 案件数据结构
 */
export const CaseDataSchema = z.object({
  id: z.string().describe('案件唯一标识'),
  title: z.string().describe('案件标题'),
  description: z.string().describe('案件描述'),
  location: z.string().describe('案件发生地点'),
  timeOfCrime: z.string().describe('案发时间'),
  victim: z.string().describe('受害者姓名'),
  murderWeapon: z.string().describe('凶器'),
  motive: z.string().describe('作案动机'),
  culpritId: z.string().describe('真凶ID'),
  difficulty: z.enum(['easy', 'normal', 'hard']).describe('案件难度'),
  createdAt: z.string().describe('创建时间'),
});

/**
 * 嫌疑人数据结构
 */
export const SuspectDataSchema = z.object({
  id: z.string().describe('嫌疑人唯一标识'),
  caseId: z.string().describe('所属案件ID'),
  name: z.string().describe('姓名'),
  personality: z.string().describe('性格特征'),
  background: z.string().describe('背景故事'),
  alibi: z.string().describe('不在场证明'),
  secrets: z.array(z.string()).describe('隐藏的秘密'),
  isCulprit: z.boolean().describe('是否为真凶'),
  appearance: z.object({
    avatar: z.string().describe('头像URL'),
    description: z.string().describe('外貌描述'),
  }).describe('外貌信息'),
  suspicionLevel: z.number().min(1).max(10).describe('可疑程度(1-10)'),
  emotionalState: z.enum(['calm', 'nervous', 'angry', 'sad', 'defensive']).describe('情绪状态'),
});

/**
 * 线索数据结构
 */
export const ClueDataSchema = z.object({
  id: z.string().describe('线索唯一标识'),
  caseId: z.string().describe('所属案件ID'),
  description: z.string().describe('线索描述'),
  type: z.enum(['physical', 'testimony', 'circumstantial', 'digital']).describe('线索类型'),
  importance: z.number().min(1).max(5).describe('重要程度(1-5)'),
  isRevealed: z.boolean().describe('是否已被发现'),
  relatedSuspects: z.array(z.string()).describe('相关嫌疑人ID列表'),
  discoveredAt: z.string().optional().describe('发现时间'),
});

/**
 * 对话记录结构
 */
export const ConversationSchema = z.object({
  id: z.string().describe('对话唯一标识'),
  caseId: z.string().describe('所属案件ID'),
  suspectId: z.string().describe('嫌疑人ID'),
  messages: z.array(z.object({
    role: z.enum(['detective', 'suspect']).describe('发言角色'),
    content: z.string().describe('对话内容'),
    timestamp: z.string().describe('发言时间'),
    emotion: z.string().optional().describe('情绪标记'),
  })).describe('对话消息列表'),
  cluesDiscovered: z.array(z.string()).describe('本次对话发现的线索ID'),
  suspicionChange: z.number().describe('可疑度变化'),
});

/**
 * AI Agent 响应结构
 */
export const AgentResponseSchema = z.object({
  content: z.string().describe('回复内容'),
  emotion: z.enum(['calm', 'nervous', 'angry', 'sad', 'defensive']).describe('情绪状态'),
  suspicionLevel: z.number().min(1).max(10).describe('当前可疑程度'),
  newClues: z.array(ClueDataSchema).describe('新发现的线索'),
  hints: z.array(z.string()).describe('给侦探的提示'),
});

/**
 * 案件生成请求
 */
export const CaseGenerationRequestSchema = z.object({
  difficulty: z.enum(['easy', 'normal', 'hard']).describe('案件难度'),
  theme: z.string().optional().describe('案件主题偏好'),
  suspectCount: z.number().min(2).max(4).default(3).describe('嫌疑人数量'),
});

/**
 * 嫌疑人对话请求
 */
export const SuspectDialogueRequestSchema = z.object({
  caseId: z.string().optional().describe('案件ID'),
  suspectId: z.string().describe('嫌疑人ID'),
  question: z.string().describe('侦探的问题'),
  conversationHistory: z.array(z.object({
    role: z.enum(['detective', 'suspect']),
    content: z.string(),
    timestamp: z.string(),
  })).optional().describe('对话历史'),
  timeRemaining: z.number().optional().describe('剩余调查时间(分钟)'),
  discoveredClues: z.array(z.string()).optional().describe('已发现的线索ID列表'),
});

/**
 * 推理结果提交
 */
export const DeductionSubmissionSchema = z.object({
  caseId: z.string().describe('案件ID'),
  accusedSuspectId: z.string().describe('指控的嫌疑人ID'),
  reasoning: z.string().describe('推理过程'),
  evidenceUsed: z.array(z.string()).describe('使用的证据ID列表'),
  confidence: z.number().min(1).max(10).describe('推理信心程度'),
});

/**
 * 游戏状态
 */
export const GameStateSchema = z.object({
  caseId: z.string().describe('当前案件ID'),
  phase: z.enum(['investigation', 'deduction', 'result']).describe('游戏阶段'),
  timeRemaining: z.number().describe('剩余时间(分钟)'),
  discoveredClues: z.array(z.string()).describe('已发现线索ID列表'),
  conversationCount: z.number().describe('对话次数'),
  suspectInteractions: z.record(z.number()).describe('与各嫌疑人的互动次数'),
  score: z.number().describe('当前得分'),
});

/**
 * 工作流状态
 */
export const WorkflowStateSchema = z.object({
  currentStep: z.string().describe('当前步骤'),
  completedSteps: z.array(z.string()).describe('已完成步骤'),
  data: z.record(z.any()).describe('工作流数据'),
  error: z.string().optional().describe('错误信息'),
});

// 导出类型定义
export type CaseData = z.infer<typeof CaseDataSchema>;
export type SuspectData = z.infer<typeof SuspectDataSchema>;
export type ClueData = z.infer<typeof ClueDataSchema>;
export type Conversation = z.infer<typeof ConversationSchema>;
export type AgentResponse = z.infer<typeof AgentResponseSchema>;
export type CaseGenerationRequest = z.infer<typeof CaseGenerationRequestSchema>;
export type SuspectDialogueRequest = z.infer<typeof SuspectDialogueRequestSchema>;
export type DeductionSubmission = z.infer<typeof DeductionSubmissionSchema>;
export type GameState = z.infer<typeof GameStateSchema>;
export type WorkflowState = z.infer<typeof WorkflowStateSchema>;